package stock_controller;

import java.sql.SQLException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import stock_service.CustomerService;
import stock_vo.CheckedContents;
import stock_vo.CustomerVO;
import stock_vo.ItemVO;

public class CustomerController extends MultiActionController{
	private CustomerService customerService;

	public void setCustomerService(CustomerService customerService) {
		this.customerService = customerService;
	}
	public ModelAndView registFormCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println("registFormCustomer controller");
		ModelAndView mv = new ModelAndView("ctjsonView");
		return mv;
	}
	/*public ModelAndView searchFormCustomer(HttpServletRequest request,
			HttpServletResponse response) throws Exception{
		System.out.println("searchFormCustomer controller");
		ModelAndView mv = new ModelAndView("stock/request/customer_select_request.tiles");
		return mv;
	}*/
	public ModelAndView insertCustomer(HttpServletRequest request,
			HttpServletResponse response, CustomerVO vo) throws Exception{
		ModelAndView mv = new ModelAndView("ctjsonView");
		System.out.println("insertCustomer controller");
		try{
			customerService.insertCustomer(vo);
			List list = customerService.selectCustomer();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	public ModelAndView deleteCustomer(HttpServletRequest request,
			HttpServletResponse response, CheckedContents contents) throws Exception{
		ModelAndView mv = new ModelAndView("ctjsonView");
		System.out.println("delete controller");
		String c[] = contents.getContents();
		System.out.println(c.length);
		try{
			for(int i=0 ; i<c.length ;i++){
				String cus_number = c[i];
				System.out.println("cus_number = "+cus_number);
				customerService.deleteCustomer(cus_number);
			}
			List list = customerService.selectCustomer();
			mv.addObject("list", list);
		}catch(SQLException se){
			se.printStackTrace();
		}
		return mv;
	}
	public ModelAndView selectCustomer(HttpServletRequest request,
			HttpServletResponse response, CustomerVO vo) throws Exception{
		System.out.println("selectCustomer controller");
		String search = request.getParameter("search");
		String url = "";
		System.out.println(search);
		ModelAndView mv=new ModelAndView();
		try{
			List list = customerService.selectCustomer();
			mv.addObject("list", list);
			System.out.println(list.size());
			url="stock/request/customer_insert_request.tiles";
		}catch(SQLException se){
			mv.addObject("info", se.getMessage());
			se.printStackTrace();
		}
		if(search!=null){
			url="customerPopup";
		}
		mv.setViewName(url);
		return mv;
	}
}